package com.xjtu.conan.controller;


import com.xjtu.conan.entity.NewsInfo;
import com.xjtu.conan.entity.TheatreInfo;
import com.xjtu.conan.entity.UserInfo;
import com.xjtu.conan.response.AjaxResponse;
import com.xjtu.conan.service.NewsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 * 新闻表 前端控制器
 * </p>
 *
 * @author 一只黑猩猩
 * @since 2021-04-19
 */
@CrossOrigin
@RestController
@RequestMapping("/conan/news-info")
@Api(value = "新闻管理模块", description = "这是新闻信息CRUD的模块")
public class NewsInfoController {

    @Autowired
    private NewsInfoService newsInfoService;

    @ApiOperation(value = "获取全部新闻", notes = "获取所有新闻的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getAllNews", method = RequestMethod.GET)
    public AjaxResponse getAllNews(@RequestParam(value = "current") Integer current,
                                   @RequestParam(value = "size") Integer size) {

        return AjaxResponse.success(newsInfoService.getAllNewsInfo(current, size));
    }

    @ApiOperation(value = "添加新闻", notes = "添加一个新闻")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nName", value = "新闻标题", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "nContent", value = "新闻内容", required = true, paramType = "form", dataType = "String"),
    })
    @RequestMapping(value = "addNews", method = RequestMethod.POST)
    public AjaxResponse addNews(@RequestParam(value = "nName") String nName,
                                @RequestParam(value = "nContent") String nContent,
                                HttpServletRequest request) throws IOException {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        NewsInfo newsInfo = new NewsInfo()
                .setNewsName(nName)
                .setNewsContent(nContent)
                .setCreateUser(1384057153556135937L)
//                .setCreateUser(loginUser.getUid())
                .setUpdateUser(1384057153556135937L);
//                .setUpdateUser(loginUser.getUid());
        return AjaxResponse.success("新增新闻ID->" + newsInfoService.addNewsInfo(newsInfo));
    }

    @ApiOperation(value = "更新新闻", notes = "录入新的新闻信息并且修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nName", value = "新闻标题", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "nContent", value = "新闻内容", required = true, paramType = "form", dataType = "String")
    })
    @RequestMapping(value = "updateNews", method = RequestMethod.PUT)
    public AjaxResponse updateNews(@RequestParam(value = "nName") String nName,
                                      @RequestParam(value = "nContent") String nContent,
                                      HttpServletRequest request) throws IOException {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        NewsInfo newsInfo = new NewsInfo()
                .setNewsName(nName)
                .setNewsContent(nContent)
                .setUpdateUser(loginUser.getUid());
        return AjaxResponse.success(newsInfoService.updateNewsInfo(newsInfo));
    }

    @ApiOperation(value = "删除新闻信息", notes = "从数据库中删除某个新闻")
    @ApiImplicitParam(name = "nid", value = "删除新闻id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteNews", method = RequestMethod.DELETE)
    public AjaxResponse deleteNews(@RequestParam(value = "nid") Long nid) {
        return AjaxResponse.success("数据库受影响行数->" + newsInfoService.deleteNewsInfo(nid));
    }

    @ApiOperation(value = "新闻总数量", notes = "获取数据库中新闻总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(newsInfoService.getTotal());
    }
}

